using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using LeisureCenter.Api;

namespace LeisureCenter.MetadataHandlers
{
    public class VideoDefaultMetadataHandler : VideoMetadataHandler
    {
        private static string ProcessString(string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 1; i < chars.Length - 1; i++)
            {
                if ((chars[i] == '.' || chars[i] == '_') && chars[i - 1] != ' ' && chars[i + 1] != ' ')
                    chars[i] = ' ';
            }
            str = new string(chars);
            while (str.Length > 0 && RemovableChar(str[0]))
                str = str.Remove(0, 1);
            while (str.Length > 0 && RemovableChar(str[str.Length - 1]))
                str = str.Remove(str.Length - 1);
            return str;
        }

        private static bool RemovableChar(char p)
        {
            return p <= 32 || p == '.' || p == '_' || p == '-';
        }

        private static string ProcessTitle(string title)
        {
            return ProcessString(title);
        }

        private static string ProcessSubtitle(string title)
        {
            return ProcessString(title);
        }

        public override void ReadMetadata(string fileName, Stream stream, bool computeDataHash)
        {
            string name = Path.GetFileNameWithoutExtension(fileName);
            Regex regex = new Regex(@"((?:se?)?(\d{1,2})(?:e|x)?(\d{2}))", RegexOptions.IgnoreCase);
            Match match = regex.Match(name);
            if (!match.Success)
            {
                this.Title = name;
            }
            else
            {
                this.Title = ProcessTitle(name.Substring(0, match.Groups[1].Index));
                this.Subtitle = ProcessSubtitle(name.Substring(match.Groups[1].Index + match.Groups[1].Length));
                if (match.Groups[2].Success)
                    this.Number = int.Parse(match.Groups[2].Value);
                if (match.Groups[3].Success)
                    this.SubNumber = int.Parse(match.Groups[3].Value);
                this.Type = VideoType.TvShow;
            }

            if (computeDataHash)
            {
                stream.Position = 0;
                this.dataHash = HashStreamFromPositionFivePercent(stream);
            }
        }
    }

    public class VideoDefaultMetadataHandlerFactory : MetadataHandlerFactory
    {
        public override bool AcceptMedia(string fileName, Stream stream)
        {
            string ext = Path.GetExtension(fileName);
            return Program.LeisureCenterInstance.Settings.Extensions.ContainsKey(ext) && Program.LeisureCenterInstance.Settings.Extensions[ext].Kind == MediaKind.Video;
        }

        public override MetadataHandler CreateMetadata()
        {
            return new VideoDefaultMetadataHandler();
        }
    }
}
